package cn.edu.imnu.service.impl;

import cn.edu.imnu.service.WordPropertyService;
import common.model.DataBootstrapTable;
import common.model.ResponseModelBootstrapTable;
import common.util.Page;
import cn.edu.imnu.mapper.WordPropertyMapper;
import cn.edu.imnu.model.WordProperty;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author ranhongmin--2018/3/3
 * Description:
 */
@Service
public class WordPropertyServiceImpl implements WordPropertyService {
	@Resource
	private WordPropertyMapper propertyMapper;
	/**
	 * 后台祝查询
	 *
	 * @param dataBootstrapTable
	 * @return
	 */
	@Override
	public ResponseModelBootstrapTable main(DataBootstrapTable dataBootstrapTable) {
		ResponseModelBootstrapTable modelBootstrapTable = new ResponseModelBootstrapTable();

		Integer offset = dataBootstrapTable.getOffset();
		Integer pageSize = dataBootstrapTable.getLimit();
		Integer currentPage = 1;
		String search = dataBootstrapTable.getSearch();

		if (offset == 0) {
			currentPage = 1;
		} else {
			currentPage = (pageSize + offset) / pageSize;
		}

		Page page = new Page(currentPage, pageSize);

		Example example = new Example(WordProperty.class);
		Example.Criteria cri = example.createCriteria();
		//没删除的
		cri.andEqualTo("isDel", "1");

		List<?> wordInfo;
		//查询
		if (search != null && !"".equals(search)) {
			cri.andLike("propertyName", "%" + search + "%");
		}
		//排序
		if (dataBootstrapTable.getSort() != null && !"".equals(dataBootstrapTable.getSort())) {
			Example.OrderBy orderBy = example.orderBy(dataBootstrapTable.getSort());
			if ("desc".equals(dataBootstrapTable.getOrder())) {
				orderBy.desc();
			} else {
				orderBy.asc();
			}
		}

		wordInfo = propertyMapper.selectByExampleAndRowBounds(example,
				new RowBounds(page.getStartLine(), pageSize));
		page.setTotalLine(propertyMapper.selectCountByExample(example));

		modelBootstrapTable.bing(wordInfo, page.getTotalLine(), page.getTotalPage());
		return modelBootstrapTable;
	}

	/**
	 * 后台动作
	 *
	 * @param property
	 * @param action
	 * @return
	 */
	@Override
	public ResponseModelBootstrapTable doAction(WordProperty property, String action) {
		ResponseModelBootstrapTable modelBootstrapTable = new ResponseModelBootstrapTable();
		try {
			switch (action) {
				case "1": {
					//	add
					property.setCreateTime(new Date());
					property.setIsDel(1);
					propertyMapper.insertSelective(property);
					modelBootstrapTable.success();
				}
				break;
				case "2": {
					//	update
					property.setUpdateTime(new Date());
					property.setIsDel(1);
					propertyMapper.updateByPrimaryKeySelective(property);
					modelBootstrapTable.success();
				}
				break;
				case "3": {
					//	del
					property.setIsDel(0);
					propertyMapper.updateByPrimaryKeySelective(property);
					modelBootstrapTable.success();
				}
				break;
				default:
					modelBootstrapTable.error();
					break;
			}
		} catch (Exception e) {
			modelBootstrapTable.error();
		}
		return modelBootstrapTable;

	}

	/**
	 * 根据词条id获取属性
	 *
	 * @param wordId
	 * @return
	 */
	@Override
	public ResponseModelBootstrapTable getPropertyByWordId(String wordId) {
		ResponseModelBootstrapTable modelBootstrapTable = new ResponseModelBootstrapTable();
		Example example = new Example(WordProperty.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("wordId",wordId);
		modelBootstrapTable.setList(propertyMapper.selectByExample(example));
		return modelBootstrapTable;
	}
}
